﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

using AutoMapper;

using KSOAdmin.Core.EFDbContext;
using KSOAdmin.Core.Expressions;
using KSOAdmin.IRepository;
using KSOAdmin.IServices.System;
using KSOAdmin.Models.DomainModels.System;
using KSOAdmin.Models.MapperModels.Enums;
using KSOAdmin.Models.MapperModels.Page;
using KSOAdmin.Models.ViewModel;
using KSOAdmin.Repository.System;

using Microsoft.EntityFrameworkCore;

using StackExchange.Redis;

namespace KSOAdmin.Services.System
{
    public class Sys_LogServices : BasicServices<Sys_Log>, ISys_LogServices, IDependency
    {
        private readonly ISys_LogRepository _sys_Log;
        private readonly IMapper _mapper;
        public Sys_LogServices(ISys_LogRepository sys_Log, IBasicRepository<Sys_Log> BaseRepository, IMapper mapper)
        {
            this.repository = BaseRepository;//父类
            this._sys_Log = sys_Log;
            this._mapper = mapper;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public async Task<PageListModel<View_Sys_Log>> SysLogPageListAsync(View_Sys_Log viewsys_Log, int pageIndex, int pageSize)
        {
            var orderByQuery = base.FindAsIQueryable()
                .WhereIf(!string.IsNullOrWhiteSpace(viewsys_Log?.Url), w => w.Url.Contains(viewsys_Log.Url))
                .WhereIf(!string.IsNullOrWhiteSpace(viewsys_Log.LogType), w => w.LogType.Contains(viewsys_Log.LogType));
            var viewquery = _mapper.ProjectTo<View_Sys_Log>(orderByQuery);

            return await base.PageOtherListAsync<View_Sys_Log>(viewquery, x => new Dictionary<object, QueryOrderBy>()
                {
                    { x.ElapsedTime,QueryOrderBy.Asc}
                }   , pageIndex, pageSize);
        }


    }
}
